/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.test;

import edu.uoc.sd.fr14.model.CustomerId;
import edu.uoc.sd.fr14.model.FlightId;
import edu.uoc.sd.fr14.model.Op;
import edu.uoc.sd.fr14.model.OpId;
import edu.uoc.sd.fr14.model.Quorum;
import edu.uoc.sd.fr14.model.ServerId;
import edu.uoc.sd.fr14.model.ServerState;

/**
 *
 * @author drodenas
 */
public class ServerStateCreator {
    
    public static ServerState createFor(ServerId serverId) {
        return new ServerStateCreator(serverId).getServerState();
    }
    
    public static final FlightId FLIGHT_CUSTOMER1000 = new FlightId(1);
    public static final FlightId FLIGHT_FULL_CUSTOMER1000 = new FlightId(2);
    public static final FlightId FLIGHT_FULL = new FlightId(3);
    public static final FlightId FLIGHT_ALMOST_FULL = new FlightId(4);
    public static final FlightId FLIGHT_EMPTY = new FlightId(5);
    
    public static final CustomerId CUSTOMER1000 = new CustomerId(1000);
    public static final CustomerId CUSTOMER1001 = new CustomerId(1001);
    
    private final ServerState serverState;

    private ServerStateCreator(ServerId serverId) {
        
        serverState = new ServerState(serverId);
        
        seat(FLIGHT_CUSTOMER1000, CUSTOMER1000);
        seat(FLIGHT_FULL_CUSTOMER1000, CUSTOMER1000);
        
        fill(FLIGHT_FULL_CUSTOMER1000);
        fill(FLIGHT_FULL);
        fill(FLIGHT_ALMOST_FULL, 1);
    }
    
    private boolean seat(FlightId flight, CustomerId customer) {

        OpId opId = serverState.getLog().annotate(flight, customer, customer);
        Op op = serverState.getLog().prepared(opId, Quorum.createFromTotal(2));
        return serverState.getLog().executeOp(op, serverState.getCompany()) != null;
    }

    private ServerState getServerState() {
        return serverState;
    }
    
    
    private void fill(FlightId flight) {
        this.fill(flight, 0);
    }
    private void fill(FlightId flight, int freeSeats) {

        int nextCustomer = 2000;
        while (serverState.getCompany().getFlight(flight).query() > freeSeats) {
            seat(flight, new CustomerId(nextCustomer));
            nextCustomer++;
        }
        
    }
    
    
    
}
